package com.yuanqiworld.vw.domain;

import java.io.File;
import java.util.Date;
import java.util.logging.FileHandler;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.Lob;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.classpath.operations.jsr303.RooUploadedFile;
import org.springframework.util.Assert;

@RooJavaBean
@RooToString
@RooJson
@RooJpaActiveRecord(table = "vw_unity_model", finders = "findUnityModelsByModelName")
public class UnityModel {

    public static final String MODEL_EXTENSION = ".unit3d";

    @NotNull
    private String modelName;

    private String modelType;

    private Integer modelVersion;

    private String modelFilename;

    private String previewFilename;

    private Long modelSize;

    private Long previewSize;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date uploadDatetime;

    private String uploadModel;

    private String uploadPreview;

    private String modelNote;

    public static TypedQuery<com.yuanqiworld.vw.domain.UnityModel> findLastUnityModelsByModelName(String modelName) {
        if (modelName == null || modelName.length() == 0) throw new IllegalArgumentException("The modelName argument is required");
        EntityManager em = UnityModel.entityManager();
        TypedQuery<UnityModel> q = em.createQuery("SELECT o FROM UnityModel AS o WHERE o.modelName = :modelName AND o.modelVersion = (SELECT max(m.modelVersion) from UnityModel AS m where m.modelName = o.modelName)", UnityModel.class);
        q.setParameter("modelName", modelName);
        return q;
    }

    public static Integer findMaxVersionByModelName(String modelName) {
        if (modelName == null || modelName.length() == 0) throw new IllegalArgumentException("The modelName argument is required");
        EntityManager em = UnityModel.entityManager();
        TypedQuery<Integer> q = em.createQuery("SELECT MAX(o.modelVersion) FROM UnityModel AS o WHERE o.modelName = :modelName", Integer.class);
        q.setParameter("modelName", modelName);
        Integer result = q.getSingleResult();
        if (result == null) return 0;
        return result;

    }

    public String getModelTypeLabel() {
        if(StringUtils.isBlank(modelType)) return null;
        TypedQuery<Metadata> query = Metadata.findMetadatasByCategoryAndMetadataValue("MODEL_TYPE", modelType);
        if(query.getResultList().size() != 1) return null;
        return query.getSingleResult().getMetadataLabel();
    }
}
